﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Core;

namespace Web.Controllers
{
    public class contestController : JsonController
    {
        //
        // GET: /contest/
        public ActionResult showContest(bool god, int start, int limit)
        {
            return Json(contest.showContestList(Certify.god,start,limit));
        }

        public ActionResult contestProblemList(int cid, bool god, string player)
        {
            return Json(contest.getContestProblemList(cid, god, player));
        }

        public ActionResult showContestProblem(int cid, char ps)
        {
            Sql.ContestProblemStatistics cps = new Sql.ContestProblemStatistics();
            var p = contest.showContestProblem(cid, ps, out cps, Certify.god);
            if (p == null) return Content("题目不存在");
            ViewData["cps"] = cps;
            return View(p);
        }
        public ActionResult getPNumber(int cid)
        {
            return Json(contest.getPNumber(cid));
        }

        public ActionResult getContestStatus(int cid,int start,int limit)
        {
            return Json(contest.showStatus(cid, start, limit));
        }
        [OnlyGod]
        public ActionResult addOrEditContest(int cid, string title, string startTime, string overTime,
            bool disabled, string _content, bool is_public)
        {
            DateTime start_time = new DateTime();
            DateTime.TryParse(startTime, out start_time);
            DateTime over_time = new DateTime();
            DateTime.TryParse(overTime, out over_time);
            return Json(contest.addOrEditContest(cid, title, start_time,
            over_time, disabled, _content, is_public));
        }

        [OnlyGod]
        public ActionResult getAutoCid()
        {
            return Json(contest.getAutoCid());
        }

        [OnlyGod]
        public ActionResult deleteContest(int cid)
        {
            return Json(contest.deleteContest(cid));
        }

        [OnlyGod]
        public ActionResult rejudgeStatus(int rid)
        {
            return Json(contest.rejudgeStatus(rid));
        }

        public ActionResult getAutoContestPid(int cid)
        {
            return Json(contest.getAutoContestPid(cid));
        }

        [ValidateInput(false)]
        [OnlyGod]
        public ActionResult conAEProblem(int cid, char ps, int? oldPid, int pid, string title, int timeLimit, int memoryLimit, string disabled,
            string description, string input, string output, string sampleInput, string sampleOutput, string source,
            int? exampleLang, string exampleCode, string inputData, string outputData,
            string uploadDataFile, HttpPostedFileBase inputDataFile, HttpPostedFileBase outputDataFile,
            string specialJudge, int? specialLang, string specialCode,string hint)
        {
            IsFileUpload = true;
            CheckEmpty(title, description, input, output, sampleInput, sampleOutput);
            if (exampleLang != null) CheckEmpty(exampleCode);
            var _uploadDataFile = uploadDataFile.ToBool();
            var _specialJudge = specialJudge.ToBool();
            if (_uploadDataFile)
            {
                CheckNull(inputDataFile, outputDataFile);
                inputData = new System.IO.StreamReader(inputDataFile.InputStream).ReadToEnd();
                outputData = new System.IO.StreamReader(outputDataFile.InputStream).ReadToEnd();
            }
            else
            {
                CheckEmpty(inputData, outputData);
                if (_specialJudge) CheckEmpty(specialLang, specialCode);
            }
            if (!inputData.EndsWith("\n")) inputData += "\r\n";
            if (!outputData.EndsWith("\n")) outputData += "\r\n";

            return Json(contest.addOrEditProblem(cid,ps,
            oldPid, pid, title, timeLimit, memoryLimit, disabled.ToBool(),
            description.TrimHtmlEnd(), input.TrimHtmlEnd(), output.TrimHtmlEnd(), sampleInput, sampleOutput, source,
            exampleLang, exampleCode, inputData, outputData,
            _specialJudge, specialLang, specialCode,hint));
        }

        [OnlyGod]
        public ActionResult deleteCProblem(int cid, char ps)
        {
            return Json(contest.deleteContestProblem(cid, ps));
        }

        [OnlyGod]
        public ActionResult getCProblemCompleteInfo(int cid,char ps)
        {
            return Json(contest.getCProblemCompleteInfo(cid, ps));
        }

        [OnlyGod]
        public ActionResult getContestCompleteInfo(int cid)
        {
            return Json(contest.getContestCompleteInfo(cid));
        }

        [ValidateInput(false)]
        [NeedLogon]
        [ParamsNoEmpty]
        public ActionResult submitContestProblem(int cid, char ps, int lang, string code, bool god)
        {
            return Json(contest.submitCode(cid, ps, lang, code, Certify.username, god));
        }
        public ActionResult showContestRanklist(int cid)
        {
            return Json(contest.showRanklist(cid));
        }
    }
}
